import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:znmm/UI/A_Common/buttons/easy_button/easy_elevated_button.dart';
import 'package:znmm/UI/A_Common/custom_checkbox.dart';
import 'package:znmm/UI/A_Common/custom_textField.dart';
import 'package:znmm/UI/utils/sharedpre_utils.dart';
import 'package:znmm/config/app_asset.dart';
import 'package:znmm/config/app_gaps.dart';
import 'package:znmm/config/app_routes.dart';
import 'package:znmm/network/notify_listener.dart';

import 'mag/login_mag.dart';
import 'register_page.dart';

//密码登录
class PwdLoginView extends StatefulWidget {
  const PwdLoginView({super.key});

  @override
  State<PwdLoginView> createState() => _PwdLoginViewState();
}

class _PwdLoginViewState extends State<PwdLoginView> {
  final GlobalKey<CustomTextFieldState> _glUserNameKey =
      GlobalKey<CustomTextFieldState>();

  final GlobalKey<CustomTextFieldState> _glPWDNameKey =
      GlobalKey<CustomTextFieldState>();

  int _iNotiId1 = -1;

  String _userNameStr = "";
  String _userPwdStr = "";

  bool _bIsremember = false;

  @override
  void initState() {
    super.initState();

    _bIsremember = SharedPrefUtil().getData(SharedKey.strRemember) ?? false;

    if (_bIsremember) {
      _userNameStr =
          SharedPrefUtil().getData(SharedKey.strLoginUserNameKey) ?? '';
      _userPwdStr = SharedPrefUtil().getData(SharedKey.strLoginPwdKey) ?? '';
    }

    // _userNameStr = "157";
    // _userPwdStr = "123";

    // _userNameStr = "demo";
    // _userPwdStr = "demo";

    //添加监听者
    _iNotiId1 = NotifyCenter().addObserver(NotifyKey.registerKey, (object) {
      _userNameStr =
          SharedPrefUtil().getData(SharedKey.strLoginUserNameKey) ?? '';
      _userPwdStr = SharedPrefUtil().getData(SharedKey.strLoginPwdKey) ?? '';

      print("_userNameStr:$_userNameStr");
      print("_userPwdStr:$_userPwdStr");

      if (_glUserNameKey.currentState != null) {
        _glUserNameKey.currentState!.setText(_userNameStr);
      }

      if (_glPWDNameKey.currentState != null) {
        _glPWDNameKey.currentState!.setText(_userPwdStr);
      }
    });
  }

  @override
  void dispose() {
    NotifyCenter().removeCallback(_iNotiId1);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        CustomTextField(
          key: _glUserNameKey,
          labelText: 'UserName',
          strText: _userNameStr,
          imageIcon: AppAsset.account,
          bHasFrame: false,
          onInputComplete: (value) {
            //print("用户名:$value");
            _userNameStr = value;
          },
        ),
        AppGaps.vGaps20w,

        CustomTextField(
          key: _glPWDNameKey,
          labelText: 'Password',
          strText: _userPwdStr,
          imageIcon: AppAsset.key,
          bHasFrame: false,
          bHasHidIcon: true,
          onInputComplete: (value) {
            //print("密码:$value");
            _userPwdStr = value;
          },
        ),
        AppGaps.vGaps5w,
        //记住密码
        Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            GestureCheckbox(
              initialValue: _bIsremember,
              strDef: "Remember password",
              fWidth: 260,
              onChanged: (value) {
                print('记住密码:$value');
                _bIsremember = value;
              },
            ),
            const Spacer(),
            // TextButton(
            //   onPressed: () {
            //     // 处理按钮点击事件
            //   },
            //   child: Text(
            //     'Forgot Password？',
            //     style: TextStyle(
            //       color: Colors.black,
            //       fontSize: 16.sp,
            //       decoration: TextDecoration.underline,
            //       decorationColor: Colors.black,
            //     ),
            //   ),
            // ),
          ],
        ),

        AppGaps.vGaps50w,
        EasyElevatedButton(
          isRounded: true,
          width: double.infinity,
          height: 44,
          label: 'Visitor Login',
          color: Colors.black,
          onPressed: () {
            //_goLogin();

            // 访客登录
            print("访客登录");
            Get.offNamedUntil(RouteNames.homeRoute, (route) => false);
          },
        ),
        AppGaps.vGaps15w,

        EasyElevatedButton(
          isRounded: true,
          width: double.infinity,
          height: 44,
          label: 'Login',
          color: Colors.black,
          onPressed: () {
            LoginMag().reqLogin(
              _userNameStr,
              _userPwdStr,
              _bIsremember,
            );

            //跳到首页
            //Get.offNamedUntil(RouteNames.homeRoute, (route) => false);
          },
        ),
        AppGaps.vGaps15w,

        EasyElevatedButton(
          isRounded: true,
          width: double.infinity,
          height: 44,
          label: 'Register',
          color: Colors.black,
          onPressed: () {
            Get.to(const RegisterPage());
          },
        ),
      ],
    );
  }
}
